jQuery Method for Replacing All Strings [Recommended]

  • 2021-08-03 08:20:53
  • OfStack

Everyone knows that jquery has an replace method that can replace "" in a string with any character, but it can only replace 1 place. If there are multiple places, it can only be handled by regular expressions


var title = "aaa aaa aaa";
var re = new RegExp(" ","g"); // Defining regular expressions 
// No. 1 1 Parameters are the contents to be replaced, and the 2 Parameters "g" Represents replacing all ( global ). 
newTitle = title!=null?title.replace(re, "_"):title;
//3 Purpose operation 

The last code is to replace "" with _ according to re regular expression rules

In addition, there are regular test tools on the front-end network. Recently, I am also continuing to learn regularity. Come on! ! !


Related articles: